#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <string.h>

#include "pwm.h"
#include "rf12.h"
#include "adc.h"

struct EngineThrust
{
	char PacketType; /* musy be first */
	unsigned char YawControl;
	unsigned char AltitudeControl;
};


struct Beep
{
	char PacketType; /* musy be first */
	unsigned char Tone;
} g_Beep;


void ProcessCommand(unsigned char *packet,unsigned char packet_len)
{

unsigned char tmp_char[50];
unsigned char tmp_len;

	switch (packet[0])
	{
		case 'A':
			/* Analaog measures */
			tmp_char[0]='A';
			GetMeasureData(tmp_char+1, &tmp_len);
			RFM12B_WritePacket((unsigned char *)tmp_char,tmp_len+1);

		break;

		case 'E':
			/* Engine control */
			PWM_SetDutyOnChanelA(((struct EngineThrust *)(packet))->YawControl);
			PWM_SetDutyOnChanelB(((struct EngineThrust *)(packet))->AltitudeControl);
		break;

		case 'B':
		    /* Beep */
			Beep(((struct Beep *)(packet))->Tone);

		break;

		case 'I':
		    /* Get integrated values */
			tmp_char[0]='I';
			GetIntegratorValues(tmp_char+1, &tmp_len);
			RFM12B_WritePacket((unsigned char *)tmp_char,tmp_len+1);

		break;

		case 'O':
		    /* Set measures offset */
			SetIntegratorZeroOffsetValues(packet+1, packet_len-1);

		break;

		case 'R':
		    /* Reset inegrated values */
			ResetIntegrators();

		break;

	}


}





void EmergencyAbort(void)
{
	PWM_SetDutyOnChanelA(0);
	PWM_SetDutyOnChanelB(0);
}
